package app.android.com.wuandroidapp.test.model.db.dao;

import android.content.Context;
import android.util.Log;

import com.db.user.entity.PassWord;

import java.util.List;


/**
 * 完成对某一张表的具体操作
 * Created by LGL on 2016/7/2.
 */
public class PasswordUtils {

    //TAG
    private static final String TAG = PasswordUtils.class.getSimpleName();

    private DaoManager daoManager;

    //构造方法
    public PasswordUtils(Context context) {
        if (daoManager == null) {
            daoManager = DaoManager.getInstance();
            daoManager.initManager(context);
        }
    }

    /**
     * 对数据库中student表的插入操作
     *
     * @param pw
     * @return
     */
    public boolean insertStudent(PassWord pw) {
        boolean flag = false;
        flag = daoManager.getDaoSession().insert(pw) != -1 ? true : false;
        return flag;
    }

    /**
     * 批量插入
     *
     * @param
     * @return
     */
    public boolean inserMultStudent(final List<PassWord> pw) {
        //标识
        boolean flag = false;
        try {
            //插入操作耗时
            daoManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (PassWord p : pw) {
                        daoManager.getDaoSession().insertOrReplace(p);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改
     *
     * @param pw
     * @return
     */
    public boolean updateStudent(PassWord pw) {
        boolean flag = false;
        try {
            daoManager.getDaoSession().update(pw);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除
     *
     * @param pw
     * @return
     */
    public boolean deleteStudent(PassWord pw) {
        boolean flag = false;
        try {
            //删除指定ID
            daoManager.getDaoSession().delete(pw);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //daoManager.getDaoSession().deleteAll(); //删除所有记录
        return flag;
    }

    /**
     * 查询单条
     *
     * @param key
     * @return
     */
    public PassWord listOneStudent(long key) {
        return daoManager.getDaoSession().load(PassWord.class, key);
    }

    /**
     * 全部查询
     *
     * @return
     */
    public List<PassWord> listAll() {
        return daoManager.getDaoSession().loadAll(PassWord.class);
    }

    /**
     * 原生查询
     */
    public void queryNative() {
        //查询条件
        String where = "where name like ? and _id > ?";
        //使用sql进行查询
        List<PassWord> list = daoManager.getDaoSession().queryRaw(PassWord.class, where,
                new String[]{"%l%", "6"});
        Log.i(TAG, list + "");
    }


}
